//
//  BHPhotoBrowser.m
//  Behing
//
//  Created by Lj on 2017/10/23.
//  Copyright © 2017年 lj. All rights reserved.
//

static NSString *const photoBrowserCell = @"BHPhotoBrowserCell";

#import "BHPhotoBrowser.h"
#import "BHPhotoBrowserCell.h"
#import "BHImageModel.h"

@interface BHPhotoBrowser ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,BrowserImageViewDelegate>
@property (nonatomic, strong) NSArray *imageArr;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *navigationBar;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, assign) BOOL isNaVHidden;
@property (nonatomic, assign) NSInteger currentIndex;


@end

@implementation BHPhotoBrowser

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (id)initWithImages:(NSArray *)images currentIndex:(NSInteger)currentIndex {
    if (self = [super init]) {
        self.imageArr = images;
        self.currentIndex = currentIndex;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setUpUI];
    [self navBarView];
}

- (void)setUpUI {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.collectionView registerClass:[BHPhotoBrowserCell class] forCellWithReuseIdentifier:photoBrowserCell];
    [self.view addSubview:_collectionView];
    
    if (_currentIndex < self.imageArr.count) {
        [_collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:_currentIndex inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionLeft];
    }
}

- (void)navBarView {
    self.navigationBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, kNavbarAndStatusBar + (kDevice_Is_iPhoneX ? 40 : 20))];
    self.navigationBar.backgroundColor = [UIColor colorWithWhite:0.5f alpha:0.5f];
    self.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:self.navigationBar];
    
    UIImageView *backImageView = [[UIImageView alloc] init];
    backImageView.image = [UIImage imageNamed:@"home-7"];
    [_navigationBar addSubview:backImageView];
    [backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.navigationBar.mas_bottom).offset(- 10);
        make.left.equalTo(self.navigationBar).offset(10);
        make.width.height.offset(24);
    }];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 20, 50, kNavbarAndStatusBar);
    [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    [_navigationBar addSubview:backBtn];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = [NSString stringWithFormat:@"%d/%ld",_currentIndex + 1,(unsigned long)self.imageArr.count];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.font = [UIFont systemFontOfSize:18];
    [_navigationBar addSubview:_titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.navigationBar);
        make.bottom.equalTo(self.navigationBar.mas_bottom).offset(-5);
    }];
    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BHPhotoBrowserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:photoBrowserCell forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[BHPhotoBrowserCell alloc]init];
    }
    cell.photoBrowser.imageDelegate = self;
    if ([self.imageArr[indexPath.item] isKindOfClass:[UIImage class]]) {
        cell.photoBrowser.showImage = self.imageArr[indexPath.item];
    }else if ([self.imageArr[indexPath.item] isKindOfClass:[NSString class]]) {
        cell.photoBrowser.showImage = self.imageArr[indexPath.item];
    }else {
        BHImageModel *imageModel = self.imageArr[indexPath.item];
        cell.photoBrowser.showImage = imageModel.image;
    }
    cell.photoBrowser.zoomScale = 1.0;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.bounds.size;
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    BHPhotoBrowserCell *photoBrowserCell = (BHPhotoBrowserCell *)cell;
    photoBrowserCell.photoBrowser.zoomScale = 1.0;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x <= -60) {
//        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.currentIndex = scrollView.contentOffset.x / App_Frame_Width;
    self.titleLabel.text = [NSString stringWithFormat:@"%ld/%ld",self.currentIndex + 1,self.imageArr.count];
    
}

#pragma mark - BrowserImageViewDelegate
- (void)deleteSelectImageView:(BHBrowserImageView *)browserImageView deleteImage:(UIImage *)currentImage {
    [UIView animateWithDuration:.1 animations:^{
        if (self.isNaVHidden) {
            _navigationBar.transform = CGAffineTransformIdentity;
        }else {
            _navigationBar.transform = CGAffineTransformMakeTranslation(0, - (kNavbarAndStatusBar + (kDevice_Is_iPhoneX ? 40 : 20)));
        }
    } completion:^(BOOL finished) {
        self.isNaVHidden = !self.isNaVHidden;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma  mark - 按钮
- (void)backClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.collectionView.frame = self.view.bounds;
    self.collectionView.contentOffset = CGPointMake(self.view.bounds.size.width * _currentIndex, 0);
    [self.collectionView reloadData];
}


@end
